// Copyright 2009 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Author: Jeff Stewart
// This is the second screen the user sees.  It asks the user to identify their
// mail reading program and the profile within that program.  Then it solicits
// their email address and password.

#ifndef POPDIAG_UI_CLIENTCONFIGSCREEN_H__
#define POPDIAG_UI_CLIENTCONFIGSCREEN_H__

#include <QtGui/QLineEdit>
#include <QtGui/QVBoxLayout>
#include <QtGui/QComboBox>
#include <QtGui/QLabel>
#include <QtGui/QCheckBox>
#include <QtGui/QtGui>

#include "util/common.h"
#include "config/configverifier.h"

namespace popdiag {

class WizardPanel;

class ClientConfigScreen : public QWizardPage {
  Q_OBJECT

 public:
  explicit ClientConfigScreen(QWidget *app);
  virtual ~ClientConfigScreen();

  void SetupUI();

  // Called each time this page is about to be displayed.  Can be used to
  // populate this page from other pages.
  virtual void initializePage();

  // Called when the user clicks Next, this can veto navigation.
  virtual bool validatePage();

  // Called immediately before this page is pulled off the screen.
  virtual void cleanupPage();

  // Called to determine if all needed fields are filled.
  virtual bool isComplete() const;

 signals:
  void ConfigChosen(VerificationResult *config);

  public slots:
  // We have configuration values.  Populate the pulldowns.
  void OnConfigResultsCollected(const VerificationSet &vs);

 private slots:
  // User picked a program from the list.  Update
  // the profile display (or show the email entry box)
  void OnProgramSelected(int index);

  // User picked a profile from the list.
  void OnProfileSelected(int index);

 private:
  // We need to manually collect an email address (and possibly servers).
  void ShowEmailAndServerFields(bool show);

  // Show or hide the profile pulldown and its label.
  void ShowProfile(bool show);

  typedef QList<VerificationResult*> ResultList;
  // A map from program name to profiles.
  typedef QMap<QString, ResultList> ResultMap;

  // The configuration check results, stored in the form
  // we want, indexed first by program name.
  ResultMap results_;

  // The config results we are selecting from.  Includes all the
  // local profiles found.  We extract email programs and profile
  // names from this.
  VerificationSet config_results_;

  // The currently selected profile.  Can be NULL.
  VerificationResult *current_result_;

  // A pulldown box listing email programs to choose from.
  QComboBox   *program_combo_;

  // The label for the list of email profiles.
  QLabel      *profile_label_;

  // A pulldown box listing email profiles within a given
  // email program.
  QComboBox   *profile_combo_;

  // The label for the box that solicits the users email address
  QLabel      *email_address_label_;

  // An example email address displayed in the UI as a hint.
  QLabel      *email_example_label_;

  // Users enter their email address into this box.  In some
  // cases this box is not displayed but is instead populated
  // from the profile pulldown box.
  QLineEdit   *email_address_field_;

  // The field where users enter their account password
  QLineEdit   *password_field_;

  // The label beside the password field entry area.
  QLabel      *password_label_;

  // Labels for the server fields.
  QLabel      *imap_server_label_;
  QLabel      *pop_server_label_;
  QLabel      *smtp_server_label_;
  QLabel      *smtp_port_example_label_;

  // Server fields.
  QLineEdit   *imap_server_field_;
  QLineEdit   *pop_server_field_;
  QLineEdit   *smtp_server_field_;
  QLineEdit   *smtp_port_field_;

  // If true, the "Next" button can be displayed.
  // See Qt Documentation for QWizardPage
  // for details on the protocol of changing the return
  // value from "isComplete()"
  bool complete_;
};

}  // namespace popdiag

#endif  // POPDIAG_UI_CLIENTCONFIGSCREEN_H__
